<?php

use Hyperf\Context\ApplicationContext;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Logger\LoggerFactory;
use Hyperf\Redis\RedisFactory;
use Hyperf\Redis\RedisProxy;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Log\LoggerInterface;

if (!function_exists('di')) {
    /**
     * Finds an entry of the container by its identifier and returns it.
     *
     * @return ContainerInterface|mixed
     */
    function di(?string $id = null)
    {
        $container = ApplicationContext::getContainer();
        if ($id) {
            return $container->get($id);
        }

        return $container;
    }
}

if (!function_exists('request')) {
    /**
     * 获取req.
     *
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    function request(): RequestInterface
    {
        return di()->get(RequestInterface::class);
    }
}

if (!function_exists('config')) {
    /**
     * 获取配置信息.
     */
    function config(string $key, mixed $default = null): mixed
    {
        return \Hyperf\Config\config($key, $default);
    }
}

if (!function_exists('redis')) {
    /**
     * 获取redis连接.
     *
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    function redis(string $poolName = 'default'): RedisProxy
    {
        return di()->get(RedisFactory::class)->get($poolName);
    }
}

if (!function_exists('logger')) {
    /**
     * 获取日志对象.
     *
     * @param string $group 日志分组名，对应config/autoload/logger.php中的日志group
     * @param string $name  记录在日志文件中的开头，名字
     */
    function logger(string $group = 'default', string $name = 'default'): LoggerInterface
    {
        return di()->get(LoggerFactory::class)->get($name, $group);
    }
}

if (!function_exists('get_real_ip')) {
    /**
     * 获取真实ip.
     *
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    function get_real_ip(): string
    {
        if ($ip = request()->getHeaderLine('x-real-ip')) {
            return $ip;
        }
        if ($ip = request()->getHeaderLine('x-forwarded-for')) {
            return $ip;
        }

        return request()->getServerParams()['remote_addr'] ?? '';
    }
}

if (!function_exists('env')) {
    /**
     * 获取环境变量信息.
     */
    function env(string $key, mixed $default = null): mixed
    {
        return \Hyperf\Support\env($key, $default);
    }
}

if (!function_exists('config')) {
    /**
     * 获取配置信息.
     */
    function config(string $key, mixed $default = null): mixed
    {
        return \Hyperf\Config\config($key, $default);
    }
}

if (!function_exists('value')) {
    /**
     * Return the default value of the given value.
     *
     * @template TValue
     * @template TReturn
     *
     * @param (Closure():TReturn)|TValue $value
     *
     * @return ($value is Closure ? TReturn : TValue)
     */
    function value(mixed $value, ...$args)
    {
        return \Hyperf\Support\value($value, ...$args);
    }
}

if (!function_exists('service_api_common')) {
    /**
     * 获取公共服务地址
     */
    function service_api_common(): string
    {
        $url = config('nacos_config.service_api_common');
        if (empty($url)) {
            throw new Exception('service_api_common is empty');
        }

        return $url.'/dtm';
    }
}

if (!function_exists('service_api_order')) {
    /**
     * 获取订单服务地址
     */
    function service_api_order(): string
    {
        $url = config('nacos_config.service_api_order');
        if (empty($url)) {
            throw new Exception('service_api_order is empty');
        }

        return $url.'/dtm';
    }
}

if (!function_exists('service_api_product')) {
    /**
     * 获取商品服务地址
     */
    function service_api_product(): string
    {
        $url = config('nacos_config.service_api_product');
        if (empty($url)) {
            throw new Exception('service_api_product is empty');
        }

        return $url.'/dtm';
    }
}
